package com.woniuxy.service.impl;

import com.woniuxy.bean.Menu;
import com.woniuxy.mapper.PermissionMapper;
import com.woniuxy.pojo.Permission;
import com.woniuxy.service.MenuService;
import com.woniuxy.util.MenuBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 根据用于的id获取后台首页左侧菜单
     * @param uid
     * @return
     */
    @Override
    public List<Menu> getIndexLeftMenusByUid(int uid) {
        List<Menu> menus=new ArrayList<Menu>();
        List<Permission> permissionList=this.permissionMapper.selectMenusByUid(uid);
        if(permissionList!=null&&!permissionList.isEmpty()){
            for(Permission permission:permissionList){
                Menu menu=new Menu();
                menu.setId(permission.getId());
                menu.setPid(permission.getPid());
                menu.setTitle(permission.getName());
                menu.setIcon(permission.getIcon());
                menu.setHref(permission.getHref());
                menu.setSpread(permission.getOpen()==1?true:false);
                menus.add(menu);
            }
        }
        return MenuBuilder.build(menus,1);
    }
}
